// pages/class/add/add.js
var userinfo = wx.getStorageSync('useridentity');
var classservice = require("../../../apiservice/class/classservice");
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userinfo: [],
		calssname: '',
		school: {},
		positionid: [],
		gradelist: [{
				name: "小班",
				value: 13
			},
			{
				name: "中班",
				value: 14
			},
			{
				name: "大班",
				value: 15
			},
			{
				name: "一年级",
				value: 1
			},
			{
				name: "二年级",
				value: 2
			},
			{
				name: "三年级",
				value: 3
			},
			{
				name: "四年级",
				value: 4
			},
			{
				name: "五年级",
				value: 5
			},
			{
				name: "六年级",
				value: 6
			},
			{
				name: "七年级",
				value: 7
			},
			{
				name: "八年级",
				value: 8
			},
			{
				name: "九年级",
				value: 9
			},
			{
				name: "高一",
				value: 10
			},
			{
				name: "高二",
				value: 11
			},
			{
				name: "高三",
				value: 12
			}
		],
		gradeName: '',
		gradeValue: 0
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options);
		this.setData({
			userinfo: userinfo,
			positionid: options
		});
		if (options.schoolname && options.schoolid) {
			var school = this.data.school;
			school.new_schoolid = options.schoolid;
			school.new_name = options.schoolname;
			this.setData({
				school: school
			})
			console.log(this.data.school);
		}
		console.log(this.data.gradelist[2].name);
	},


	onChange: function(e) {
		this.setData({

			calssname: e.detail
		})

	},
	toSchoollist: function() {
		console.log("跳转到学校详细列表");
		// if (this.data.school.new_name && this.data.school.new_name != '请选择')
		// 	return;
		wx.navigateTo({
			url: '../../school/list/list?type=choose',
		});
	},
	changeGrade: function(e) {
		this.setData({
			gradeValue: this.data.gradelist[parseInt(e.detail.value)].value,
			gradeName: this.data.gradelist[parseInt(e.detail.value)].name
		});
		console.log(this.data.gradeValue);
	},
	//添加班级信息
	addClass: function() {
		console.log(this.data.school);
		var newname = this.data.school.new_name + this.data.calssname;
		var classobj = {
			new_classnumber: this.data.calssname,
			new_name: newname,
			userid: this.data.userinfo.userid,
			new_school: this.data.school.new_schoolid,
			new_grade: this.data.gradeValue
		}
		// console.log(classobj.new_school);
		var msg = '';
		var isCreate = true;
		var flag = true;
		if (classobj.new_name.replace(/\s+/g, '') == '' && flag == true) {
			msg = '请输入班级名称';
			isCreate = false;
			flag = false;
		}
		if (classobj.new_school == undefined && flag == true) {
			msg = '请选择学校';
			isCreate = false;
			flag = false;
		}
		if (classobj.new_grade == 0 && flag == true) {
			msg = '请选择年级';
			isCreate = false;
			flag = false;
		}
		if (isCreate == false) {
			wx.showToast({
				title: msg,
				icon: 'none',
				duration: 2000
			})
		} else {
			console.log("grade:" + classobj.new_grade);
			this.addClassInfo(classobj);
		}

	},
	//添加班级信息
	addClassInfo: function(obj) {
		var that = this;
		classservice.addClass(obj).then(function(success) {
			console.log(success);
			that.showResult(success.data);
			wx.navigateBack({
				url: '../list/list'
			})
		}).catch(function(ex) {
			console.log(ex);
		})
	},
	showResult: function(res) {
		var title = '';
		var icon = '';
		if (res.resultState == 1) {
			title = '创建成功';
			icon = 'success';
		} else {
			title = '创建失败';
			icon = 'none';
		}
		wx.showToast({
			title: title,
			icon: icon,
			duration: 2000
		});
	},
})
